PowerWEB File Upload for ASP.NET
Streaming File Data
Send comments on this topic.



Glossary Item Box

A unique feature of PowerWEB File Upload is the ability to interact with and process file data during an upload operation. While not necessarily a feature that all users require, it makes possible solutions that perform tasks such as providing a seamless upload from client browser to a destination FTP server.

By default, the UploadManager is configured to stream file bytes directly to the server file system. This greatly reduces server overhead and limits the memory utilization to a small 50K buffer for each in-progress file upload. Traditional HTTP upload methods require that the entire file be loaded into server memory before it is made available to the ASP.NET page cycle.

If you handle the UploadManager.UploadProgress event, the UploadProgressEventArgs expose a Data property. This property is a byte array containing all bytes for a given file since the last UploadProgress event was fired. Once the byte array is accessed, it is removed from server memory so as to keep memory utilization to a minimum. Because it is possible for many files to use the same name, a UniqueID property is also exposed on the UploadProgressEventArgs to assist with synchronizing multiple uploads and reassembling byte arrays.

The following example code-behind demonstrates how to use these events and properties to directly place uploaded file data onto an open Stream. The full sample source code can be found in the sample project included with PowerWEB File Upload:

C# Copy Code
private void UploadManager1_UploadProgress(object sender, Dart.FileUpload.UploadProgressEventArgs e)
 {
     string filename = e.FileName;
     string id = e.UniqueID;
     System.IO.BinaryWriter bw = new System.IO.BinaryWriter(GetStream(id,"STREAMED." + filename));
     bw.Write(e.Data);
 }

 private void UploadManager1_FileUploadComplete(object sender, Dart.FileUpload.FileUploadEventArgs e)
 {
     System.IO.Stream s = GetStream(e.PostedFile.UniqueID,e.PostedFile.FileName);
     if (s != null) s.Close();
 }

 private System.IO.Stream GetStream(string id, string filename)
 {
     System.IO.Stream s = (System.IO.Stream)Session[id];
     if (s == null)
     {
         s = new System.IO.FileStream(Server.MapPath("uploaded_files/" + filename), System.IO.FileMode.Create,
                 System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);
         Session[id] = s;
     }
     return s;
 }

Visual Basic Copy Code
'Requires Imports Dart.FileUpload

 Private Sub UploadManager1_UploadProgress(ByVal sender As Object, ByVal e As UploadProgressEventArgs) _
         Handles UploadManager1.UploadProgress
     Dim filename As String = e.FileName
     Dim id As String = e.UniqueID
     Dim bw As New System.IO.BinaryWriter(GetStream(id, "STREAMED." + filename))
     bw.Write(e.Data)
 End Sub

 Private Sub UploadManager1_FileUploadComplete(ByVal sender As Object, ByVal e As FileUploadEventArgs) _
         Handles UploadManager1.UploadComplete
     Dim s As System.IO.Stream = GetStream(e.PostedFile.UniqueID, e.PostedFile.FileName)
     If (Not s Is Nothing) Then s.Close()
 End Sub

 Private Function GetStream(ByVal id As String, ByVal filename As String) As System.IO.Stream
     Dim s As System.IO.Stream = CType(Session(ID), System.IO.Stream)
     If (s Is Nothing) Then
         s = New IO.FileStream(Server.MapPath("uploaded_files/" + filename), IO.FileMode.Create, _
                 IO.FileAccess.ReadWrite, IO.FileShare.Read)
         Session(ID) = s
     End If
     Return s
 End Function
Documentation Version 4.0.3
© 2012 Dart Communications. All Rights Reserved.